﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls;
using System.Windows.Threading;


namespace DIST.LR.ZHJG.Controls.Chart
{
    public partial class ucPie : UserControl
    {
        public delegate void PieAreaSelectChanged(string name);
        public event PieAreaSelectChanged PieAreaSelectChangedHandler;

        public ucPie()
        {
            InitializeComponent();

            #region 不需要的
            ucPieViewModel pieDataModel = new ucPieViewModel();
            //this.RadChart1.ItemsSource = pieDataModel.PieData;

            PieValue pievalue = new PieValue()
            {
                LegendLabelName = "Catagory",
                YValueName = "Value",
                XCategoryName = "Catagory"
            };

            PieInfo<PieItem> pieInfo = new PieInfo<PieItem>();
            pieInfo.PieSetting = pievalue;
            pieInfo.DataSource = pieDataModel.PieData;
            Binding(pieInfo);
            #endregion

        }

        public void Binding<T>(PieInfo<T> pieinfo)
        {
            this.RadChart1.SeriesMappings.Clear();
            SeriesMapping mapping = new SeriesMapping();
            mapping.SeriesDefinition = new PieSeriesDefinition()
            {
                ShowItemToolTips = true,
                ShowItemLabels=true,
                RadiusFactor = 0.7,
                ItemLabelFormat = "#Y",//"#XCAT #Y"
                LabelSettings = new RadialLabelSettings() 
                { 
                    LabelOffset = 0.8, 
                    ShowConnectors = false, 
                    SpiderModeEnabled = false 
                },
                InteractivitySettings = new InteractivitySettings() 
                { 
                    SelectionMode=Telerik.Windows.Controls.Charting.ChartSelectionMode.Single,
                    HoverScope = Telerik.Windows.Controls.Charting.InteractivityScope.None,
                    SelectionScope = Telerik.Windows.Controls.Charting.InteractivityScope.Item
                },
                SeriesItemLabelStyle = this.Resources["PieSeriesLabelStyle"] as Style
            };
            //mapping.LegendLabel = "fff";//pieinfo.PieSetting.LegendLabelName;
            mapping.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.YValue, FieldName = pieinfo.PieSetting.YValueName });
            mapping.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.XCategory, FieldName = pieinfo.PieSetting.XCategoryName });
            mapping.ItemMappings.Add(new ItemMapping() { DataPointMember = DataPointMember.LegendLabel, FieldName = pieinfo.PieSetting.LegendLabelName });
            this.RadChart1.SeriesMappings.Add(mapping);
            this.RadChart1.ItemsSource = pieinfo.DataSource;
        }

        private void ChartAreaSelectionChanged(object sender, ChartSelectionChangedEventArgs e)
        {
            string name = null;
            if (e.AddedItems.Count > 0)
            {
                name = e.AddedItems[0].XCategory;
            }
            
            if (PieAreaSelectChangedHandler != null)
            {
                PieAreaSelectChangedHandler(name);
            }
        }
    }

    public class PieInfo<T>
    {
        public PieValue PieSetting { get; set; }
        public List<T> DataSource { get; set; }
    }

    public class PieValue
    {
        public string LegendLabelName { get; set; }
        public string YValueName { get; set; }
        public string XCategoryName { get; set; }
    }
}
